﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using ARTemplate;
using System.IO;
using System.Drawing.Imaging;
using Newtonsoft.Json;
using Tools;
using System.Text.RegularExpressions;

namespace ScanTemplate
{    
    public class Optionsubjects
    {
        public Optionsubjects()
        {
            OptionSubjects = new List<Optionsubject>();
        }
        public void Clear()
        {
        }
        public void Add(Optionsubject S)
        {
            OptionSubjects.Add(S);
        }
        public List<Optionsubject> OptionSubjects { get; set; }

        public void InitDeserialize()
        {
            if (OptionSubjects != null)
                foreach (Optionsubject O in OptionSubjects)
                    O.InitDeserialize();
        }
    }
    public class Optionsubject
    {
        public Optionsubject()
        {
        }
        public Optionsubject(SingleChoiceArea U, int index, int pos)
        {
            OptionAnswerScore O = U.Listanswerscore[pos];
            this.Index = O.Index;
            this.ID = O.ID;
            this.Score = O.Score;
            this.Answer = O.Answer;
            this.Type = XztQuestion.CharType( O.Type );
            InitDeserialize(U, pos);
        }
        private void InitDeserialize(SingleChoiceArea U, int pos)
        {
            this._Rect = U.ImgArea;
            this.Size = U.Size;
            this.List = new List<Point>();
            List = U.list[pos];            
        }
        public void InitDeserialize()
        {
            if (Type == null)
                Type = "S";
            else if ("SMU".Contains(Type))
                this.Type = XztQuestion.CharType(  Type );
        }
        public override string ToString()
        {
            return Name();
        }

        public int ID { get; set; }
        public float Score { get; set; }
        public int Index { get; set; }
        public List<Point> List{ get; set; }
        public Size Size { get; set; }
        public string Name() { return "x" + ID; }
        public string OutName { get { return "选择题" + ID; } }
        public string Answer { get; set; }

        public float HalfScore { get; set; }
        public string Type { get; set; }
               
        [JsonProperty]
        private Rectangle _Rect;

        [JsonIgnore]
        public Rectangle Rect { get { return _Rect; } }
        [JsonIgnore]
        public int Height { get { return Rect.Height; } }
        [JsonIgnore]
        public int Width { get { return Rect.Width; } }
        //[JsonIgnore]
        //private SingleChoiceArea U;
    }
    public class XztQuestion
    {
        public int ID { get; set; }
        public string OptionAnswer { get; set; }
        public float Score { get; set; }
        public string Type { get; set; }
        public float HalfScore { get; set; }

        public string TypeName()
        {
            if (Type == "S")
                return "单选";
            if (Type == "M")
                return "多选";
            if (Type == "U")
                return "不定项";
            Type = "S";
            return "单选";
        }
        public static string CharType(string Type)
        {
            if ("SMU".Contains(Type))
                return Type;
            if (Type == "单选")
                return "S";
            if (Type == "多选")
                return "M";
            if (Type == "不定项")
                return "U";
            return "S";

        }
    }

    [JsonObject(MemberSerialization.OptIn)]
    public class TzOptionsubjects
    {
        public TzOptionsubjects()
        {
            Tzs = new List<TzOptionsubject>();
        }
        [JsonProperty]
        public List<TzOptionsubject> Tzs;
        public void Add(TzOptionsubject tz)
        {
            Tzs.Add(tz);
        }
        public void InitIds()
        {
            foreach (TzOptionsubject tz in Tzs)
                tz.InitIds();
        }
        public void Deserialize(Optionsubjects optioinsubjects)
        {
            foreach (TzOptionsubject r in Tzs)
                r.InitDeserialize(optioinsubjects);
        }
    }

    [JsonObject(MemberSerialization.OptOut)]
    public class TzOptionsubject
    {
        public TzOptionsubject()
        {
            Subjects = new List<Optionsubject>();
            SubjectIndexs = new List<int>();
        }
        public void Add(Optionsubject tz)
        {
            Subjects.Add(tz);
        }
        public void InitIds()
        {
            SubjectIndexs.Clear();
            if (Subjects.Count > 0)
                SubjectIndexs.AddRange(
                    Subjects.Select(r => r.ID).ToArray());
        }
        public void InitDeserialize(Optionsubjects ibs)
        {
            Subjects.Clear();
            if (SubjectIndexs.Count > 0)
                Subjects = SubjectIndexs.Select(r => ibs.OptionSubjects[r]).ToList();
        }
        [JsonIgnore]
        public List<Optionsubject> Subjects;
        [JsonProperty]
        public List<int> SubjectIndexs;
        public Rectangle Rect;
        public string Name;

        public void SetIndexList(List<int> list)
        {
            SubjectIndexs.Clear();
            SubjectIndexs.AddRange(list);
        }
    }

    [JsonObject(MemberSerialization.OptIn)]
    public class Tzsubjects
    {
        public Tzsubjects()
        {
            Tzs = new List<Tzsubject>();
        }
        [JsonProperty]
        public List<Tzsubject> Tzs;
        public void Add(Tzsubject tz)
        {
            Tzs.Add(tz);
        }
        public void InitIds()
        {
            foreach (Tzsubject tz in Tzs)
                tz.InitIds();
        }
        public void Deserialize(Imgsubjects imgsubjects)
        {
            foreach (Tzsubject r in Tzs)
                r.InitDeserialize(imgsubjects);
        }
    }

    [JsonObject(MemberSerialization.OptOut)]
    public class Tzsubject
    {        
        public Tzsubject()
        {
            Subjects = new List<Imgsubject>();
            Subjectids = new List<int>();
        }
        public void Add(Imgsubject tz)
        {
            Subjects.Add(tz);
        }
        public void InitIds()
        {
            Subjectids.Clear();
            if(Subjects.Count>0)
            Subjectids.AddRange(
                Subjects.Select(r => r.ID).ToArray());     
        }
        public void InitDeserialize(Imgsubjects ibs)
        {
            Subjects.Clear();
            if(Subjectids.Count>0)
            Subjects = Subjectids.Select(r => ibs.ImgSubjectByID(r)).ToList();
        }
        [JsonIgnore]
        public bool BPage
        {
            get
            {
                if (Subjects == null || Subjects.Count == 0) return false;
                return Subjects[0].BPage;
            }
        }
        [JsonIgnore]
        public List<Imgsubject> Subjects;
        [JsonProperty]
        private List<int> Subjectids;
        public Rectangle Rect;
        public string Name;
    }

    [JsonObject(MemberSerialization.OptOut)]
    public class Imgsubjects
    {
        public Imgsubjects()
        {
            _subjects = new List<Imgsubject>();
            _dic = new Dictionary<int, Imgsubject>();
            _activeid = -1;
        }
        public void InitDeserialize()
        {
            _dic = new Dictionary<int, Imgsubject>();
            _activeid = -1;
            foreach (Imgsubject S in _subjects)
            {
                _dic[S.ID] = S;
                S.InitDeserialize();
            }
        }
        public bool Add(Imgsubject S)
        {
            if (!_dic.ContainsKey(S.ID)) //FormYJInit.InitImgSubjects() 中 设定
            {
                _subjects.Add(S);
                _dic[S.ID] = S;
                return true;
            }
            return false;

        }
        public Imgsubject ImgSubjectByID(int ID)
        {
            if (_dic.ContainsKey(ID))
                return _dic[ID];
            return null;
        }
        public void SetActiveSubject(Imgsubject S)
        {
            if (S != null)
                _activeid = S.ID;
            else
                _activeid = -1;
        }
        [JsonIgnore]
        public List<Imgsubject> Subjects
        {
            get
            {
                return _subjects;
            }
        }
        [JsonIgnore]
        public Imgsubject ActiveSubject
        {
            get
            {
                return ImgSubjectByID(_activeid);
            }
        }

        [JsonProperty]
        private List<Imgsubject> _subjects;
        private Dictionary<int, Imgsubject> _dic;
        private int _activeid;
    }
    public class Imgsubject
    {      
        public Imgsubject(UnChoose U, int index)
        {
            this._U = U;
            this.ID = index+1;
            this.Index = index;
            this.Score = U.Scores;
            this.Name = U.Name;
            this._Rect = _U.ImgArea;
            this.BPage = _U.BPage;
        }
        public Imgsubject()
        {
        }
        public void InitDeserialize()
        {
            int w = Width;
            w = (w / 8) * 8 + (w % 8 > 2 ? 8 : 0);
            int stride = w / 8;
            stride = (stride / 4 * 4) + (stride % 4 > 0 ? 4 : 0);
            BitmapdataLength = w * stride;
        }
        public override string ToString()
        {
            return Name;
        }
        public bool BPage { get; set; }
        public string Name { get; set; } 
        public int ID { get; set; }
        public int Score { get; set; }
        public int Index { get; set; }
        [JsonProperty]
        private Rectangle _Rect;
        [JsonIgnore]
        public Rectangle Rect { get { return _Rect; } }
        [JsonIgnore]
        public int Height { get { return Rect.Height; } }
        [JsonIgnore]
        public int Width { get { return Rect.Width; } }
        [JsonIgnore]
        public int BitmapdataLength{ get; set; }
        private UnChoose _U;
        //public int ID;
        //public Double Score;
    }
        
}
